package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {


    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    private final String prefix ="dish_";


    /**
     * 新增菜品
     * @param
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CachePut(cacheNames = prefix,key = "dishDTO.getCategoryId()")
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
  /*      // 清除部分redis 缓存
        String pattern="dish_"+dishDTO.getCategoryId();
        redisCacheClear(pattern);*/
        return Result.success("新增菜品和对应口味成功！！");
    }


    /**
     * 分页菜品 查询参数： name , categoryId  status
     * @param dishPageQueryDTO
     * @return Result
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> getDishList(DishPageQueryDTO dishPageQueryDTO){
        log.info("进行菜品分页查询，接收到参数：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.selectDishByNameAndCategoryIdAndStatus(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 删除菜品
     * @param
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result<String> delDish(@RequestParam List<Long> ids){
        log.info("删除菜品id：{}",ids.toString());
        dishService.delWithFlavor(ids);
        return Result.success("删除菜品和对应口味列表成功！！");
    }


    /**
     * id查询菜品
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("修改菜品")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("查询菜品id：{}",id);
        DishVO dishVO=dishService.selectOneById(id);
        log.info("查询成功：{}",dishVO);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param
     * @return
     */
    @PutMapping
    @ApiOperation("更新菜品")
    @Cacheable(cacheNames = prefix,key = "#dishDTO.getCategoryId()")
    public Result<String> update(@RequestBody DishDTO dishDTO){
        log.info("更新菜品数据：{}",dishDTO);
        dishService.updateDish(dishDTO);
   /*     // 清除全部redis 缓存
        String pattern="dish_*";
        redisCacheClear(pattern);*/
        return Result.success("更新菜品数据成功！");
    }


    /**
     * 菜品的 停售起售
     * @param
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品售卖状态")
    @CacheEvict(cacheNames = prefix,allEntries = true)
    public Result<String> changeStatus(@PathVariable Integer status,Long id){
        log.info("修改id:{}菜品售卖状态：{}",status,id==1?"起售":"停售");
        dishService.changeStatusById(id,status);
     /*   // 清除全部redis 缓存
        String pattern="dish_*";
        redisCacheClear(pattern);*/
        return Result.success("更新菜品数据成功！");
    }



    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        List<DishVO> list = dishService.listWithFlavor(dish);

        return Result.success(list);
    }




    /**
     *  指定模式，清除redis 缓存
     * @param pattern
     */
    public void redisCacheClear(String pattern){
        // 指定配置规则，查询key集合
        Set keys = redisTemplate.keys(pattern);
        // 批量删除 key
        redisTemplate.delete(keys);
    }



}
